package com.wen.image;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

import cn.wen.base.adapter.viewpager.QuickPagerAdapter;
import cn.wen.base.adapter.viewpager.QuickPagerHolder;
import cn.wen.base.image.ImageLoader;
import cn.wen.base.view.CompatViewPager;
import cn.wen.core.banner.BannerCreator;
import cn.wen.core.titlebar.DefTitleBar;
import cn.wen.core.titlebar.TitleBarUtil;
import cn.wen.core.ui.AppActivity;


/**
 * 照片预览
 */
public class PreviewActivity extends AppActivity {
    public static final String POSITION = "position";
    private CompatViewPager mViewPager;
    // ArrayList<String> 已经选择好的图片
    private ArrayList<String> mResultList;
    private int position;

    @Override
    protected Object getContentLayout() {
        return R.layout.activity_preview;
    }


    @Override
    protected void initData(Bundle bundle) {
        mResultList = getIntent().getStringArrayListExtra(ImageHelper.EXTRA_DEFAULT_SELECTED_LIST);
        position = getIntent().getIntExtra(POSITION, 0);
        TitleBarUtil.setStatusBarColor(this);
    }


    @Override
    protected void initTitle(DefTitleBar titleBar) {
        titleBar.setBgColor(Color.parseColor("#55000000"));
        changeTitle(position);
    }

    private void changeTitle(int position) {
        if (mResultList != null && mResultList.size() > 0) {
            mTitleBar.setTitle((position % mResultList.size() + 1) + "/" + mResultList.size());
        }
    }

    @Override
    protected ViewGroup getTitleParent() {
        ViewGroup view = findViewById(R.id.preview_title);
        TitleBarUtil.setTitlePadding(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        mViewPager = findViewById(R.id.view_pager);
        BannerCreator<String> creator = new BannerCreator<String>() {
            @Override
            protected void onBindData(QuickPagerHolder<String> holder, Context context, int position, String url) {
                ImageLoader.displayImage((ImageView) holder.getView(R.id.iv_banner), url);
                holder.setOnClickListener(R.id.iv_banner, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }

            @Override
            protected int getLayout() {
                return R.layout.item_preview_banner;
            }
        };
        mViewPager.setAdapter(new QuickPagerAdapter<>(mResultList, creator, false));
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                changeTitle(position);
            }
        });
        mViewPager.setCurrentItem(position, false);
    }


}
